// pages/kanjPage/kanjPage.js
import {
  Get,
  Post,
  JsonPost
} from "../../utils/request.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_id:null,
    info:{},
    clickGuig:'',
    guiArr:[],
    bargaining_id:null,
    onePrice:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      goods_id:options.goods_id
    })
    this.getInfo()
  },


  getInfo(){
    JsonPost('bargaining/goodsbargaining',{'goods_id':this.data.goods_id}).then(res=>{
      let arr=[]
      res.data[0].goods_spec.forEach(ele => {
        arr.push({
          name:ele.color+' | '+ele.size,
          bargaining_id:ele.bargaining_id,
          onePrice:ele.spec_price,
        })
      });
      this.setData({
        info:res.data[0],
        clickGuig:arr[0].name,
        onePrice:arr[0].onePrice,
        bargaining_id:arr[0].bargaining_id,
        guiArr:arr,
      })
    })
  },

  clickG(){
    let that=this
    let arr=[]
    this.data.guiArr.forEach(ele => {
      arr.push(ele.name)
    });
    wx.showActionSheet({
      itemList: arr,
      success (res) {
        console.log(res.tapIndex)
        that.setData({
          clickGuig:that.data.guiArr[res.tapIndex].name,
          bargaining_id:that.data.guiArr[res.tapIndex].bargaining_id,
          onePrice:that.data.guiArr[res.tapIndex].onePrice,
        })
      },
      fail (res) {
        console.log(res.errMsg)
      }
    })
  },

  goMyKan(){
    wx.navigateTo({
      url: '/pages/mykanj/mykanj',
    })
  },

  biginKan(){
    JsonPost('bargaining/initiate',{'bargaining_id':this.data.bargaining_id}).then(res=>{
      if (res.code==0) {
        wx.showToast({
          title: '发起砍价成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: '/pages/yaoKan/yaoKan?id='+this.data.bargaining_id+'&self_id='+res.self_id+'&ifKan=true',
        })
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})